/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.ByteArrayInputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.jet.JETEmitter;

import edu.pku.sei.modeler.codegen.Activator;
import edu.pku.sei.modeler.jet.model.BundleInfo;
import edu.pku.sei.modeler.jet.util.ModelerConstants;

/**
 * @author zl
 * 
 */
public class GenBundleCode extends AbstractGenerator {
	private String NL = "\r\n";
	private BundleInfo bInfo;
	
	public GenBundleCode(BundleInfo bInfo) {
		this.bInfo = bInfo;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.pku.sei.modeler.jet.codegen.AbstractGenerator#generateCode(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void generateCode(IProgressMonitor monitor)
			throws InterruptedException {
		assert bInfo != null;
		try {
			genActivator(monitor);
			genPluginFile(monitor);
			genManiFest(monitor);
		} catch(CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("generated bundle file failed");
		}
	}

	private translated.ActivatorGen genActivator = translated.ActivatorGen.create("\n");
	protected void genActivator(IProgressMonitor monitor) throws InterruptedException {
		try {
			String codeBase = ModelerConstants.pkgBase + "." + ModelerConstants.bundleBase;
			
			IFolder folder = proj.getFolder("src/"+codeBase.replace('.', '/'));
			folder.create(true, true, monitor);
			
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/bundle/Activator.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
//			String result = emitter.generate(monitor, new Object[]{bInfo.getBundleID()});
			
			String result = genActivator.generate(bInfo.getBundleID());
			IFile javaFile = folder.getFile(ModelerConstants.activatorName+".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
		} catch(Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate Activator file failed");
		}
	}
	protected void genPluginFile(IProgressMonitor monitor) throws CoreException {
		IFile file = proj.getFile("plugin.xml");
		String plugin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + NL;
		plugin += "<?eclipse version=\"3.2\"?>" + NL;
		plugin += "<plugin>";
		plugin += "    <extension" + NL;
        plugin += "        id=\""+bInfo.getBundleID()+".start\"" + NL;
        plugin += "        name=\""+bInfo.getBundleName()+"Start\"" + NL;
        plugin += "        point=\"org.eclipse.ui.startup\">" + NL;
        plugin += "        <startup class=\""+ModelerConstants.pkgBase+"."+ModelerConstants.internalBase+"."+ModelerConstants.starupName+"\"/>" + NL;
        plugin += "    </extension>" + NL;
		plugin += "</plugin>";

		ByteArrayInputStream is = new ByteArrayInputStream(plugin.getBytes());
		file.create(is, true, monitor);
	}

	protected void genManiFest(IProgressMonitor monitor) throws CoreException {
		// IFolder folder = proj.getFolder("META-INF");
		IFile file = proj.getFile("META-INF/MANIFEST.MF");
		String manifest = "Manifest-Version: 1.0" + NL;
		manifest += "Bundle-ManifestVersion: 2" + NL;
		manifest += "Bundle-Name: " + bInfo.getBundleName() + NL;
		manifest += "Bundle-SymbolicName: "+bInfo.getBundleID()+";singleton:=true"
				+ NL;
		manifest += "Bundle-Version: 1.0.0" + NL;
		manifest += "Bundle-Activator: "+ModelerConstants.pkgBase+"."+ModelerConstants.bundleBase+"."+ModelerConstants.bundleClsName + NL;
		manifest += "Bundle-Localization: plugin" + NL;
		manifest += "Bundle-Vendor: "+bInfo.getVendorName() + NL;
		manifest += "Require-Bundle: org.eclipse.ui," + NL;
		manifest += " org.eclipse.ui.ide," + NL;
		manifest += " org.eclipse.gef," + NL;
		manifest += " org.eclipse.draw2d," + NL;
		manifest += " org.eclipse.core.runtime," + NL;
		manifest += " org.eclipse.core.resources," + NL;
		manifest += " edu.pku.sei.gefcommon," + NL;
		manifest += " edu.pku.sei.modeler.runtime," + NL;
		manifest += " edu.pku.sei.notation.runtime," + NL;
		manifest += " org.eclipse.emf.common," + NL;
		manifest += " org.eclipse.jdt.core," + NL;
		manifest += " org.eclipse.ui.views" + NL;
		manifest += "Eclipse-LazyStart: true" + NL;
		ByteArrayInputStream is = new ByteArrayInputStream(manifest.getBytes());
		file.create(is, true, monitor);
	}
}
